//
//  XSJPersonalVC.m
//  ZF
//
//  Created by apple on 2019/3/11.
//  Copyright © 2019 hyy. All rights reserved.
//

#import "XSJPersonalVC.h"
#import "XSJPersonalCentralTableCell.h"
#import "SVProgressHUD.h"
#import "MJExtension.h"
#import "TZImagePickerController.h"
#import "UserInfoModel.h"
#import "BRPickerView.h"
#import "TextInputVC.h"


@interface XSJPersonalVC ()<TextInputVCDelegate>

@end

@implementation XSJPersonalVC

static NSString *const XSJPersonalCentralTableCellID = @"XSJPersonalCentralTableCellID";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    [ZFTool isHiddenNavigationBarSeparatorLine:YES vc:self];
    self.title = @"个人资料";
    [self setupTableView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAippear:(BOOL)animated{
    
    [super viewWillAppear:animated];
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //[self loadData];
}

//加载数据
//-(void)loadData
//{
//    ZWeakSelf
//    [http_my getUserBasic:[UserInfoModel getUID] viewedUserId:[UserInfoModel getUID] success:^(id responseObject)
//
//     {
//         [weakSelf loadData_ok:responseObject];
//
//     } failure:^(NSError *error) {
//
//         [SVProgressHUD showInfoWithStatus:error.domain];
//     }];
//}

//加载数据完成
//-(void)loadData_ok:(id)responseObject
//{
//    if (kObjectIsEmpty(responseObject))
//    {
//        return;
//    }
//
//    //jsonToModel
//    self.userInfo = [UserInfoModel mj_objectWithKeyValues:responseObject];
//
//    [self.tableView reloadData];
//
//}


- (void)setupTableView
{
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.backgroundColor = TableViewBGColor;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.alwaysBounceVertical=NO;
    
    [self.tableView registerClass:[XSJPersonalCentralTableCell class] forCellReuseIdentifier:XSJPersonalCentralTableCellID];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 3;
    }
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (indexPath.section==0)
    {
        XSJPersonalCentralTableCell* pcell = [tableView dequeueReusableCellWithIdentifier:XSJPersonalCentralTableCellID];
        pcell = [[XSJPersonalCentralTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJPersonalCentralTableCellID];
        if (indexPath.row==0) {
            pcell.title = @"头像";
            pcell.isShowButton = YES;
            //pcell.iconURL = self.userInfo.picture;
        }
        else if (indexPath.row==1) {
            pcell.title = @"昵称";
            pcell.isShowTitleButton = YES;
            //pcell.name = self.userInfo.name;
        }
        else if (indexPath.row==2) {
            pcell.title = @"签名";
//            if ( kStringIsEmpty(self.userInfo.style) )
//            {
//                pcell.name = @"暂无签名";
//            }
//            else
//            {
//                pcell.name = self.userInfo.style;
//            }
            pcell.isShowTitleButton = YES;
        }
        cell = pcell;
    }
    else if (indexPath.section == 1)
    {
        XSJPersonalCentralTableCell* pcell = [tableView dequeueReusableCellWithIdentifier:XSJPersonalCentralTableCellID];
        pcell = [[XSJPersonalCentralTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XSJPersonalCentralTableCellID];
        if (indexPath.row==0) {
            pcell.title = @"性别";
            pcell.iconName = [self.userInfo getSexText];
            pcell.isShowButton = YES;
        }
        else if (indexPath.row==1) {
            pcell.title = @"生日";
            //pcell.name = self.userInfo.location;
            pcell.isShowTitleButton = YES;
        }
        else if (indexPath.row==2) {
            pcell.title = @"电话号码";
            //pcell.name = [NSString stringWithFormat:@"LV.%ld",self.userInfo.level];
            pcell.isShowTitleButton = YES;
        }
        else if (indexPath.row==3) {
            pcell.title = @"城市";
            //pcell.name = self.userInfo.location;
            pcell.isShowTitleButton = YES;
        }
        cell = pcell;
    }
    
    return cell;
}


//每行的高度是多少
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

#pragma mark - TableViewDelegate
//点击了哪个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        if (indexPath.row==0) {
            //[self changeIcon];
        }
        else if (indexPath.row==1)
        {
            //修改昵称
            TextInputVC* vc = [[TextInputVC alloc]init];
            vc.title = @"修改昵称";
            vc.defalutPlaceholder = @"请输入昵称";
            vc.max_len = 20;
            vc.tagType = indexPath.row;
            vc.delegate = self;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row==2)
        {
            //个性签名
            TextInputVC* vc = [[TextInputVC alloc]init];
            vc.title = @"个性签名";
            vc.defalutPlaceholder = @"你可以说的什么!";
            vc.max_len = 50;
            vc.tagType = indexPath.row;
            vc.delegate = self;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row==0)
        {
            //性别
//            ZWeakSelf
//            [BRStringPickerView showStringPickerWithTitle:@"性别" dataSource:@[@"男",@"女"] defaultSelValue:[self.userInfo getSexText] isAutoSelect:NO themeColor:RGBColorHex(0x5081F1) resultBlock:^(id selectValue)
//             {
//                 //调接口修改
//                 UserInfoModel* m = [[UserInfoModel alloc]init];
//                 if ([selectValue isEqualToString:@"男"])
//                 {
//                     m.gender = @"1";
//                 }
//                 else
//                 {
//                     m.gender = @"2";
//                 }
//                 [weakSelf edit:m];
//             }];
        }
        else if (indexPath.row==3)
        {
            //位置
//            ZWeakSelf
//            [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeCity dataSource:nil defaultSelected:nil isAutoSelect:NO themeColor:nil resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area)
//             {
//                 NSLog(@"省[%@]：%@，%@", @(province.index), province.code, province.name);
//                 NSLog(@"市[%@]：%@，%@", @(city.index), city.code, city.name);
//                 NSLog(@"区[%@]：%@，%@", @(area.index), area.code, area.name);
//
//                 NSString* str = [province.name stringByReplacingOccurrencesOfString:@"省" withString:@""];
//                 NSString* str2 = [city.name stringByReplacingOccurrencesOfString:@"市" withString:@""];
//                 if ([province.name isEqualToString:@"北京市"]
//                     || [province.name isEqualToString:@"天津市"]
//                     || [province.name isEqualToString:@"重庆市"]
//                     || [province.name isEqualToString:@"上海市"])
//                 {
//                     //调接口修改
//                     UserInfoModel* m = [[UserInfoModel alloc]init];
//                     m.location = [NSString stringWithFormat:@"%@",str];
//                     [weakSelf edit:m];
//                 }
//                 else
//                 {
//                     //调接口修改
//                     UserInfoModel* m = [[UserInfoModel alloc]init];
//                     m.location = [NSString stringWithFormat:@"%@%@",str,str2];
//                     [weakSelf edit:m];
//                 }
//
//             } cancelBlock:^{
//                 NSLog(@"点击了背景视图或取消按钮");
//             }];
        }
    }
}

//点击了昵称、简介修改界面的保存
- (void)TextInputVCDidSave:(NSString *)text vc:(UIViewController*)vc
{
//    TextInputVC* tvc = (TextInputVC*)vc;
//    if (tvc.tagType==1)
//    {
//        if (kStringIsEmpty(text))
//        {
//            [SVProgressHUD showInfoWithStatus:@"姓名不能为空"];
//            return;
//        }
//        //调接口修改
//        UserInfoModel* m = [[UserInfoModel alloc]init];
//        m.name = text;
//        [self edit:m];
//    }
//    else if (tvc.tagType==6)
//    {
//        //调接口修改
//        UserInfoModel* m = [[UserInfoModel alloc]init];
//        m.style = text;
//        [self edit:m];
//    }
}


//-(void)edit:(UserInfoModel*)m
//{
//    m.ID = self.userInfo.ID;
//    //调修改接口
//    ZWeakSelf
//    [SVProgressHUD showWithStatus:@"正在加载"];
//    [http_my updateUserInfo:m success:^(id responseObject)
//    {
//        [SVProgressHUD dismiss];
//        [weakSelf edit_success:responseObject];
//
//    } failure:^(NSError *error)
//    {
//        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
//    }];
//}

//-(void)edit_success:(id)responseObject
//{
//    [self loadData];
//}

#pragma mark -- 方法
//- (void)changeIcon{
//    ZWeakSelf
//    [[JumpVCTool sharedManager] to_MediaCameraVC:self maxCount:1 isVideo:NO isClip:YES isAlert:YES completion:^(NSArray * _Nonnull array, NSError * _Nonnull error)
//     {
//         if (kArrayIsEmpty(array)==NO)
//         {
//             [weakSelf uploadAvatar:[array firstObject]];
//         }
//     }];
//}

//-(void)uploadAvatar:(UIImage*)image
//{
//    UserInfoModel* m = [[UserInfoModel alloc]init];
//    m.ID = self.userInfo.ID;
//    m.imageAvatar = image;
//    ZWeakSelf
//    [SVProgressHUD showWithStatus:@"正在加载"];
//    [http_my uploadAvatar:m success:^(id responseObject)
//     {
//         [SVProgressHUD dismiss];
//         [weakSelf edit_success:responseObject];
//     } failure:^(NSError *error) {
//         [SVProgressHUD showErrorWithStatus:error.domain];
//     }];
//}


@end
